// const aliOSS = {
//   accessKeyID: 'LTAIqr1k9kpr3aQT',
//   accessKeySecret: 'iwGDzruuECj5FZbPUhTtlFuNlLduuT',
//   host: 'https://ycat.oss-cn-shenzhen.aliyuncs.com',
//   timeout: 87600,
// };

export function uploadFileAliOss({ filePath, aliOSS }) {
    return new Promise(function (resolve, reject) {
        if (!filePath) {
            reject({
                status: false,
                err: '文件错误',
            })
            return
        }

        //文件后缀
        let dotIndex = filePath.lastIndexOf('.')
        let barIndex = filePath.lastIndexOf('/')
        let filePathLength = filePath.length
        let suffix = filePath.substring(dotIndex, filePathLength)
        let fileName = filePath.substring(barIndex + 1, filePathLength)
        let fileKey = new Date().getTime() + suffix

        // 获取阿里云oss签名信息
        let {
            region,
            bucket,
            accessKeyId,
            accessKeySecret,
            policy,
            signature,
            stsToken,
        } = aliOSS
        let serverUrl = `https://${bucket}.${region}.aliyuncs.com`
        let timeout = aliOSS.timeout || 87600

        wx.uploadFile({
            url: serverUrl,
            filePath: filePath,
            name: 'file',
            formData: {
                key: fileName,
                OSSAccessKeyId: accessKeyId,
                policy,
                signature,
                'x-oss-security-token': stsToken, // 访问是使用STS临时授权，则需要指定该项为SecurityToken的值
                success_action_status: '200',
            },
            success: function (res) {
                console.log('---阿里云上传文件---', JSON.stringify(res))
                if (res.errMsg === 'uploadFile:ok' && res.statusCode === 200) {
                    let url = serverUrl + '/' + fileName
                    // console.log("---文件地址---", url);
                    resolve({
                        status: true,
                        data: { url },
                        err: '',
                    })
                } else {
                    reject({
                        status: false,
                        statusCode: res.statusCode
                    })
                }
            },
            fail: function (err) {
                reject({
                    status: false,
                    err,
                })
            },
        })
    })
}
